//----------------------------------------------------------------------------------------
// @Project Includes
#include "includes.h"

//----------------------------------------------------------------------------------------
// @Local Macro Define in the module

//----------------------------------------------------------------------------------------
// @Local Type defines in the module

//----------------------------------------------------------------------------------------
// @Global Variables define in the module
static uint32_t  fac_us = 0;
static uint32_t  fac_ms = 0;

//----------------------------------------------------------------------------------------
// @Prototypes of functions in the module

//----------------------------------------------------------------------------------------
/**
 * @brief  driver systick sleep
 * @param  none
 * @return none
 * @note   none
 */
void drv_systick_sleep(void)
{
	
}

/**
 * @brief  driver systick wakeup
 * @param  none
 * @return none
 * @note   none
 */
void drv_systick_wakeup(void)
{
		drv_systick_init();	
}

void drv_systick_delay_ms(uint32_t nms)
{
    uint16_t i;
    uint16_t count_1 = (nms / 100);
    uint16_t count_2 = (nms % 100);	
    uint32_t temp;
    if(0 == count_1)
    {
				/*Reload the Microsecond Counts*/
				SysTick->LOAD = fac_ms*nms;
				/*Clear the Systick Value register*/
				SysTick->VAL =0x00;
				/*Enable Systick*/
				SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk;
				do
				{
						temp=SysTick->CTRL;
				}while((temp&0x01)&&!(temp&(1<<16)));
				/*Close Systick when the time is up*/
				SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk;
				/*Clear the Systick value register*/
				SysTick->VAL =0X00;
    }
    else
    {
        for(i=0;i<count_1;i++)
        {
					/*Reload the Microsecond Counts*/
					SysTick->LOAD=fac_ms*100;
					/*Clear the Systick Value register*/
					SysTick->VAL =0x00;
					/*Enable Systick*/
					SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk;
					do
					{
							temp=SysTick->CTRL;
					}while((temp&0x01)&&!(temp&(1<<16)));
					/*Close Systick when the time is up*/
					SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk;
					/*Clear the Systick value register*/
					SysTick->VAL =0X00;
        }
        if(count_2 != 0)
        {
					/*Reload the Microsecond Counts*/
					SysTick->LOAD=fac_ms*count_2;
					/*Clear the Systick Value register*/
					SysTick->VAL =0x00;
					/*Enable Systick*/
					SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk;
					do
					{
							temp=SysTick->CTRL;
					}while((temp&0x01)&&!(temp&(1<<16)));
					/*Close Systick when the time is up*/
					SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk;
					/*Clear the Systick value register*/
					SysTick->VAL =0X00;
        }
    }	
	
}

void drv_systick_delay_us(uint32_t nus)
{
    uint32_t temp;
    /*Reload the Millisecond Counts*/
    SysTick->LOAD=nus*fac_us;
    /*Clear the Systick Value register*/
    SysTick->VAL=0x00;
    /*Enable Systick*/
    SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ;
    do
    {
        temp = SysTick->CTRL;
    }
    while((temp&0x01) && !(temp&(1<<16)));
    /*Close Systick when the time is up*/
    SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk;
    /*Clear the Systick value register*/
    SysTick->VAL =0X00;
}

/**
 * @brief  driver systick initialization
 * @param  none
 * @return none
 * @note   none
 */
void drv_systick_init(void)
{
    /*Select HCLK as Systick clock*/
    SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
    /* Systick Count for Microsecond*/
    fac_us = (SystemCoreClock / (1000000));
    /* Systick Count for Millisecond*/
    fac_ms = (fac_us * 1000);
}

// =======================================================================================
// End of file.
// =======================================================================================
